var t = getApp()
Page({
  data: {
    list:[],
    showNewPower: false,
    KeyboardState: false,
    phone_type:'',
    name:'',
    tel:'',
    carnum: [],
    provinces: [
      ['京', '沪', '粤', '津', '冀', '晋', '蒙', '辽'],
      ['新', '吉', '黑', '苏', '浙', '皖', '闽', '赣'],
      [ '鲁', '宁','豫', '鄂', '湘' ,'桂', '琼', '渝'],
      ['川', '贵', '青', '云', '藏','陕', '甘'],
    ],
    letters:[
      ["A", "B", "C", "D", "E", "F","G", "H"],
      ["J", "K", "L", "M", "N", "P","Q", "R"],
      ["S", "T", "U", "V", "W", "X","Y", "Z"],
    ],
    numbers: [
      ["1", "2", "3", "4", "5", "6", "7", "8"],
      ["9", "0", "A", "B", "C", "D", "E", "F"],
      ["G", "H", "J", "K", "L", "M", "N", "P"],
      ["Q", "R", "S", "T", "U", "V", "W", "X"],
      ["Y", "Z", "港", "澳", "警" ,"学", "领"]
    ],
    show:false
  },
  onLoad: function (options) {},
  onShow(e){
    if (t.globalData.show_tel_login) {
      t.showTelLogin()
      return;
    }
    this.getList()
  },
  onRefresh(){
    //在当前页面显示导航条加载动画
    // wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    this.getList()
  },
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
    setTimeout(function () {
      // 不加这个方法真机下拉会一直处于刷新状态，无法复位
      wx.stopPullDownRefresh()
    }, 1000)
  },
  toaddcar(){
    wx.navigateTo({
      url: '/pages/personCenter/myCar/addCar/addCar',
    })
  },
  // 添加车辆
  add_car(e){
    this.setData({
      show:true,
      KeyboardState:true,
      choose_num:this.data.carnum.length
    })
  },
  // 添加车辆提交
  submit_add_car(e){
    var that = this
    var carnum = this.data.carnum.toString().replace(/,/g, "")
    console.log(this.data.carnum);
    if (this.data.showNewPower) {
      if (carnum.length!==8){
        wx.showToast({
          title: '请输入正确的新能源车牌号',
          icon:'none'
        })   
        return;
      }
    }else{
      if (carnum.length!==7){
        wx.showToast({
          title: '请输入正确的车牌号',
          icon:'none'
        })   
        return;
      }
    }
    var o ={
      phone:wx.getStorageSync('tel'),
      code:	carnum,
    }
    t.request('/cars/add',o,function(e){
      console.log(e);
      if (e.code==200) {
        wx.showToast({
          title: '添加成功',
        })
        that.exit()
        that.getList()
      }else{
        wx.showToast({
          title: e.msg,
          icon:'none'
        })
      }
    })

  },
  // 退出车辆添加
  exit() {
      this.setData({
      show:false,
      KeyboardState: false,
      choose_num:null,
    })
  },
  getList(e){
    var that = this
    t.request('/cars/list',{},function(res){
      console.log(res);
      if (res.code==200) {
          if (res.result.total>0) {
              t.globalData.carList = res.result
              wx.setStorageSync('carList', res.result);
              that.setData({
                list:res.result.list
              })
          }else{
            that.setData({
              list:res.result.list
            })
            wx.setStorageSync('carList',"");
            t.globalData.show_login = true
          }
      }
  })
  },
  delete_car(e){
    var that = this
    wx.showModal({
      title:'温馨提示',
      content:'确定删除该车辆吗',
      success(res){
        if (res.confirm) {
          t.request('/cars/delete',{car_id:e.currentTarget.dataset.car_id},function(e){
            console.log(e);
            if (e.code==200) {
                wx.showToast({
                  title: '删除成功',
                })
              that.getList()
            }else if(e.code==400){
              wx.showModal({
                title: e.msg,
                showCancel:false,
                complete:function(){
                }
              })
              /*wx.showToast({
                title: '请至少保留一条车辆信息！',
              })*/
            }
          })
        }else{

        }
      }
  })

  },
  // ===============================
  bindChoose(e) {
    if (this.data.carnum.length<7 || (this.data.showNewPower && this.data.carnum.length<8)) {
      var arr = [];
      arr[0] = e.target.dataset.val;
      this.data.carnum = this.data.carnum.concat(arr)
      this.setData({
        carnum: this.data.carnum,
        choose_num:this.data.carnum.length
      })
    }
  },
  bindDelChoose() {
    if (this.data.carnum.length != 0) {
      this.data.carnum.splice(this.data.carnum.length - 1, 1);
      this.setData({
        carnum: this.data.carnum,
        choose_num:this.data.carnum.length
      })
    }
  },
  // 燃油、新能源 切换
  showPowerBtn(e) {
    console.log(e.detail.value)
    this.setData({
      showNewPower: !this.data.showNewPower,
      KeyboardState: true,
      carnum:[],
      choose_num:0
    })
  },

  openKeyboard() {
    this.setData({
      KeyboardState: true,
      choose_num:this.data.carnum.length
    })
  },

  input_name(e){
    this.setData({
      name:e.detail.value
    })
  },
  input_tel(e){
    this.setData({
      tel:e.detail.value
    })
  },
  // 添加无牌车
  add_car_no(){
    wx.showModal({
      title:'提示',
      content:'您确认要添加无牌车吗？',
      success(e){
        if (e.confirm) {
          var o ={
            phone:wx.getStorageSync('tel'),
            code:	'',
            no_code:1,
          }
          t.request('/cars/add',o,function(e){
            if (e.code==200) {
              wx.showToast({
                title: '添加成功',
              })
              that.getList()
            }else{
              wx.showToast({
                title: e.msg,
                icon:'none'
              })
            }
          })
        }else{

        }
      }
    })
  },
})